package com.example.jacky.uilibrary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by jacky on 2017/8/4.
 * banker developer. <br/>
 * <br/>
 */

public class DialView extends View {
    private static String TAG = "DialView";
    private final Context mContext;

    private int mThemeColor;
    private int mDefaultThemeColor;//默认主题色
    private Paint mTextPaint;
    private Paint mArcPaint;//最上面弧线的画笔
    private int mWidth;
    private int mHeight;
    private RectF mArcRect;
    private int mArcCenterX;
    private int mArcCenterY;
    private float mArcRectRadius;//圆弧半径长度
    private float mArcWidth;//圆弧的弧宽
    private float mRatio;//宽高比例

    public DialView(Context context) {
        this(context, null);
    }

    public DialView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DialView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mRatio = 525.f / 525.f;

        //初始化一些默认的参数
        mDefaultThemeColor = Color.parseColor("#2EC3FD");
        mThemeColor = mDefaultThemeColor;

        //文字画笔
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(25f);
        mTextPaint.setColor(mThemeColor);

        //圆弧的画笔
        mArcPaint = new Paint();
        mArcPaint.setColor(mThemeColor);//画笔颜色
        mArcPaint.setAntiAlias(true);//抗锯齿
        mArcPaint.setStyle(Paint.Style.STROKE);//空心
        mArcPaint.setDither(true);//防抖动
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //绘制圆弧里面的文字
        canvas.drawText("增长率：" + 90 + "%", mArcCenterX, mArcCenterY - 60f, mTextPaint);
        canvas.drawText("40", mArcCenterX, mArcCenterY + mArcRectRadius - 20f, mTextPaint);

        //圆弧
        mArcPaint.setColor(Color.RED);
        canvas.drawArc(mArcRect, 120, 60, false, mArcPaint);
        mArcPaint.setColor(Color.BLUE);
        canvas.drawArc(mArcRect, 180, 180, false, mArcPaint);
        mArcPaint.setColor(Color.GREEN);
        canvas.drawArc(mArcRect, 360, 60, false, mArcPaint);

        canvas.save();
        canvas.translate(mArcCenterX, mArcCenterY);
        // 画刻度线
        Paint tmpPaint = new Paint(mTextPaint); //小刻度画笔对象
        tmpPaint.setStrokeWidth(1);
        tmpPaint.setColor(Color.RED);
        mTextPaint.setColor(Color.BLACK);

        float startY = mArcRectRadius - 20f;
        float stopY = startY + 10f;
        int count = 60; //总刻度数

        for (int i = 0; i < count; i++) {
            if (i >= 5 && i <= 55) {
                if (i % 5 == 0) {
                    canvas.drawLine(0f, startY - 20f, 0f, stopY + 10f, tmpPaint);
                } else {
                    canvas.drawLine(0f, startY, 0f, stopY, mTextPaint);
                }
            }
            canvas.rotate(360 / count, 0f, 0f); //旋转画纸
        }
        // 10个数字
        float txtSize = 35f;
        tmpPaint.setTextSize(txtSize);
        tmpPaint.setStrokeWidth(3.09f);
        int radius = (int) startY;
        float rR = radius - txtSize;
        for (int i = 0; i < 12; i++) {
            String txt;
            if (i == 0) {
                continue;
            } else {
                txt = String.valueOf(Math.abs(i - 11));
            }

            canvas.drawText(txt,
                    (float) (rR * Math.sin(i * 30 * Math.PI / 180f)),
                    (float) (rR * Math.cos(i * 30 * Math.PI / 180f) + 10),
                    tmpPaint);
        }

        //绘制指针
        tmpPaint.setColor(Color.GRAY);
        tmpPaint.setStrokeWidth(4);
        canvas.drawCircle(0, 0, 7, tmpPaint);
        tmpPaint.setStyle(Paint.Style.FILL);
        tmpPaint.setColor(Color.CYAN);
        canvas.drawCircle(0, 0, 5, tmpPaint);
        canvas.drawLine(0, 10, 0, -65, tmpPaint);
        canvas.restore();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

        mArcCenterX = (int) (mWidth / 2.f);
        mArcCenterY = mArcCenterX;
        mArcRectRadius = 125.f / 450.f * mWidth;

        mArcRect = new RectF();
        mArcRect.left = mArcCenterX - mArcRectRadius;
        mArcRect.top = mArcCenterY - mArcRectRadius;
        mArcRect.right = mArcCenterX + mArcRectRadius;
        mArcRect.bottom = mArcCenterY + mArcRectRadius;

        mArcWidth = 20.f / 450.f * mWidth;
        //画笔的宽度一定要在这里设置才能自适应
        mArcPaint.setStrokeWidth(mArcWidth);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int defaultWidth = Integer.MAX_VALUE;
        int width;
        int height;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        //  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //  int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
            width = widthSize;
        } else {
            width = defaultWidth;
        }
        int defaultHeight = (int) (width * 1.f / mRatio);
        height = defaultHeight;
        setMeasuredDimension(width, height);
    }
}
